package com.csun.cmny.provider.utils;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.OmcBizException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    public static String[] forTimeByYearsAndMonths(Integer years,Integer months){

        String startTime = "";
        String endTime = "";
        String theMonths="";

        if (months<10){
            theMonths="0"+months;
        }else {
            theMonths=""+months;
        }

        if (theMonths.equals("02")){
            if (years%4==0){
                startTime = years+"-"+theMonths+"-01 00:00:00";
                endTime = years+"-"+theMonths+"-29 23:59:59";
            }else {
                startTime = years+"-"+theMonths+"-01 00:00:00";
                endTime = years+"-"+theMonths+"-28 23:59:59";
            }
        }else if (theMonths.equals("04")||theMonths.equals("06")||theMonths.equals("09")||theMonths.equals("11")){
            startTime = years+"-"+theMonths+"-01 00:00:00";
            endTime = years+"-"+theMonths+"-30 23:59:59";
        }else {
            startTime = years+"-"+theMonths+"-01 00:00:00";
            endTime = years+"-"+theMonths+"-31 23:59:59";
        }
        String[] dates={startTime,endTime};
        return dates;
    }

    public static String[] forTimeByDateThreeDays(String date){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date time2 = null;
        try {
            time2 = format.parse(date);
        } catch (ParseException e) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080021);
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(time2);
        calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
        Date time3 =calendar.getTime(); //这个时间就是日期往后推一天的结果

        Calendar calendar1 = new GregorianCalendar();
        calendar1.setTime(time2);
        calendar1.add(calendar.DATE,-1);
        Date time1 = calendar1.getTime();

        String date1 = format.format(time1);
        String date2 = format.format(time2);
        String date3 = format.format(time3);

        String[] theDates={date1,date2,date3};
        return theDates;
    }
}
